package com.hedonihilist.example.service;

import com.hedonihilist.example.domain.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @description: 这个Service用于提供用户信息，供Spring Security使用，在实际使用过程中，用户信息可以从数据库中查出
 * @author: z
 * @time: 2021/9/13 上午12:01
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        // 这里假装有两个用户
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("abcde");
        User user1 = new User("user1", password);
        User user2 = new User("user2", password);

        if (s.equals("user1")) {
            return user1;
        }
        if (s.equals("user2")) {
            return user2;
        }
        return null;
    }
}
